package com.aishua.aishuawallet.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.aishua.aishuawallet.Constant;
import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.utils.CacheUtils;
import com.aishua.aishuawallet.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by aishua on 2018/7/31.
 */

public class GuideActivity extends BaseActivity {

    private static final String TAG = "GuideActivity-";
    private ViewPager vp_guide;
    private List<ImageView> imageViews;
    private LinearLayout guide_ll_indiactor;
    private Button btn_guide;
    private ImageView img_select;
    private RelativeLayout rl_guide;
    //两白点的间距
    private int distance = 35;
    //白点的直径
    private int diameter = 30;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initData();
        initView();

    }

    private void initData() {

        imageViews = new ArrayList<>();
        ImageView imguide1 = new ImageView(this);
        ImageView imguide2 = new ImageView(this);
        ImageView imguide3 = new ImageView(this);
        imguide1.setBackground(getResources().getDrawable(R.drawable.b,null));
        imguide2.setBackground(getResources().getDrawable(R.drawable.b,null));
        imguide3.setBackground(getResources().getDrawable(R.drawable.b,null));

        imageViews.add(imguide1);
        imageViews.add(imguide2);
        imageViews.add(imguide3);


    }

    private void initView() {

        rl_guide = findViewById(R.id.rl_guide);
        vp_guide = findViewById(R.id.vp_guide);
        guide_ll_indiactor =findViewById(R.id.guide_ll_indiactor);
        img_select =findViewById(R.id.img_select);
        btn_guide =findViewById(R.id.btn_guide);
        vp_guide.setAdapter(new GuideAdapter());
        vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //获取针对在父控件中的View参数，黑点
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) img_select.getLayoutParams();
                //黑点滑动距离对应的坐标 = 引导页位置*两白点的间距 + 屏幕滑动百分比*两白点的间距
                params.leftMargin = position * distance + (int) (positionOffset * distance);
                //设置黑点移动距离
                img_select.setLayoutParams(params);

            }

            @Override
            public void onPageSelected(int position) {

                if (position == imageViews.size() - 1) {
                    //滑到最后一个引导页时显示btn
                    btn_guide.setVisibility(View.VISIBLE);
                    rl_guide.setVisibility(View.GONE);
                } else {
                    btn_guide.setVisibility(View.GONE);
                    rl_guide.setVisibility(View.VISIBLE);

                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        LogUtils.v(TAG +"distance:"+distance);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(diameter, diameter);
        for (int i=0;i < imageViews.size();i++){

            ImageView indiactor = new ImageView(this);
            indiactor.setBackground(getResources().getDrawable(R.drawable.guide_normal));
            //单位为像素，需适配
            //除第一个点外，其他点均离左边的点diameter远
            if (i != 0) {

                params.rightMargin = distance;
            }
            indiactor.setLayoutParams(params);
            guide_ll_indiactor.addView(indiactor);

        }

        img_select.setBackground(getResources().getDrawable(R.drawable.guide_select));

        //监听视图树改变状态，得到当前屏幕信息
        img_select.getViewTreeObserver().addOnGlobalLayoutListener(new GuideOnGlobalLayoutListener());

        btn_guide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //保存'进入过引导页面'数据到Sp
                CacheUtils.putBoolean(GuideActivity.this, Constant.ISMAIN, true);
                //跳转到MainActivity
                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();

            }
        });
    }

    public class GuideAdapter extends PagerAdapter{

        @Override
        public int getCount() {

            LogUtils.v(TAG +"size:"+imageViews.size());
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view ==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

              ImageView imageView = imageViews.get(position);
              container.addView(imageView);
            LogUtils.v(TAG+"positon："+position);
            return imageView;


        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            ImageView imageView=(ImageView)object;
            container.removeView(imageView);

        }
    }

    private class GuideOnGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {

        @Override
        public void onGlobalLayout() {
            //两白点的间距 = 第1个点距左屏边缘 - 第0个点距左屏边缘
            distance = guide_ll_indiactor.getChildAt(1).getLeft() - guide_ll_indiactor.getChildAt(0).getLeft();
            //会执行多次，得到数据后移除监听优化性能
            img_select.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    }

}
